package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class Server {
    private ServerSocket serverSocket;//总机
    private Map<String ,PrintWriter> allOut = new HashMap<>();

    public Server() {
        try {
            System.out.println("正在启动服务器");
            serverSocket = new ServerSocket(9527);
            serverSocket = new ServerSocket(8080);
            System.out.println("服务器创建完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void start(){
        int i=1;
        while (true){
            try {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println("第"+i+"位一个客户端连接了");
                i++;
                ClientHandler clientHandler = new ClientHandler(socket);
                Thread t = new Thread(clientHandler);
                t.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) {
        Server se = new Server();
        se.start();



    }

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;
        private String nickName;


        public ClientHandler(Socket socket) {
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }

        public void run(){
            PrintWriter pw = null;
            try {
                InputStream is = socket.getInputStream();
                InputStreamReader or = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader br= new BufferedReader(or);


                OutputStream os = socket.getOutputStream();
                OutputStreamWriter ows = new OutputStreamWriter(os,StandardCharsets.UTF_8);
                BufferedWriter bos = new BufferedWriter(ows);
                pw = new PrintWriter(bos,true);

                nickName = br.readLine();
                synchronized (allOut) {
                    allOut.put(nickName,pw);
                }
                System.out.println("上线了,当前在线人数:"+allOut.size());
                String str;

                while ((str=br.readLine())!=null){
                    if (str.startsWith("@")){
                        sendMessageToSomeone(str);
                    }else {
                        sendMessageToAll("说:"+str);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                synchronized (allOut) {
                    allOut.remove(nickName);
                }
                System.out.println("下线了,当前在线人数:"+allOut.size());
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void sendMessageToAll(String message){
            System.out.println(nickName+"["+host+"]"+message);
            synchronized (allOut) {
                for (PrintWriter o:allOut.values()){
                    o.println(nickName+"["+host+"]"+message);

                }
            }
        }
        public void sendMessageToSomeone(String message){
            if (message.matches("@.+:.+")){
                String tonickname = message.substring(1,message.indexOf(":"));
                if (allOut.containsKey(tonickname)){
                    PrintWriter pw = allOut.get(tonickname);
                    String toMessage = message.substring(message.indexOf(":")+1);
                    pw.println(nickName+"悄悄对你说:"+toMessage);
                }else {
                    PrintWriter pw = allOut.get(nickName);
                    pw.println("该用户"+ tonickname +"不在线");
                }
            }else{
                PrintWriter pw = allOut.get(nickName);
                pw.println("你的私聊格式不正确");
            }

        }
    }

}
